using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKT.Support;

namespace Petra.Gis.DataServices.WKT
{
	public partial class GeometryFactory
	{
    /***********************************************
     * 28.8.2007
     * 
     * creates Point
     * from wkb blob
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    //private static GeometryBase ToPoint(ref string inputStr, System.Type parentGeomType)
    private static GeometryBase ToPoint(RegexGeometry rg, System.Type parentGeomType)
    {
      //rgp = new RegexGeometryParser(WKTGeometryType.Point);
      //rg = rgp.ParseGeometry(ref inputStr);

      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(rg, Dimension.dim2D);
      Point oGeom = new Point();
      oGeom.coordList = Coords;

      return oGeom;
/*

      //get point coordinates
      string pattern = @"(?<whitespace>\s*)" +
        @"(?<geometryname>"+getWktName[WKTGeometryType.Point]+")" + //5-18 chars
        @"(?<openpoint>\s*\()" +
        @"(?<coordinates>(\s*|\s*\,{1}\s*|\s+)\-?(\d*\.?\d+|\d+\.?\d*)([eE][+-]?\d+)?){2}" +
        @"(?<closepoint>\s*\))"; //+
      //@"(?<invalid>[^\s]+)"; //put all regex exps here to avoid invalid chars in input string
      Match m;

      Regex r = new Regex(pattern);

      m = r.Match(inputStr);
      if(m.Length<1)
        throw new ArgumentException("invalid input string for wkt","inputStr");

      //NumberFormatInfo nfi = new NumberFormatInfo();
      //nfi.NumberDecimalSeparator = ".";
      //todo:manage cultures

      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(m.Groups["coordinates"], Dimension.dim2D);
      Point oGeom = new Point();
      oGeom.origin = Coords;

      return oGeom;
 */
    }

    private static Point ToPoint(RegexGeometry rg, int start, WKTGeometryType parentGeomType)
    {

      Point pt = new Point();
      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(rg, start, 1, Dimension.dim2D);
      pt.coordList = Coords;
      return pt;  
    }
    
    /*
    private static Point ToPoint(CaptureCollection cc, int start, int end, System.Type parentGeomType)
    {

      Point pt = new Point();
      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(cc, start, end, Dimension.dim2D);
      pt.origin = Coords;
      return pt;

    }
     */





    /***********************************************
     * 28.8.2007
     * 
     * creates wkT string
     * from Point
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    private static string PointToString(GeometryBase geometry) { 

      Point _tmpPoint = (Point)geometry;

      System.IO.StringWriter mStrWriter = new System.IO.StringWriter();


      mStrWriter.Write(WKTGeometryType.Point.ToString());
      mStrWriter.Write("(");
      mStrWriter.Write(CoordinateListConverter.ToString(_tmpPoint.coordList, Dimension.dim2D));
      mStrWriter.Write(")");

      return mStrWriter.ToString();
    }
	}
}
